package com.cc.dao.impl;

import com.cc.dao.StudentDao;
import com.cc.poko.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.SQLException;

/**
 * @author cc
 * @date 2021/8/11 22:58
 * @Description
 */

public class StudentDaoImpl implements StudentDao {
    QueryRunner queryRunner;

    public StudentDaoImpl() {
    }

    public StudentDaoImpl(QueryRunner queryRunner) {
        this.queryRunner = queryRunner;
    }

    @Override
    public Student Findbyid(String id) {
        Student student= null;
        try {
             student = queryRunner.query("select * from student where id =?", new BeanHandler<Student>(Student.class), id);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return student;
    }

    @Override
    public int addstudent(Student student) {
        int update = 0;
        try {
             update = queryRunner.update("insert into student values (?,?,?,?)", student.getId(), student.getName(), student.getClassid(), student.getPassword());

        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return update;
    }

    @Override
    public int delectStudnet(String id) {
        int delect = 0;
        try {
            delect = queryRunner.update("delete from student where id =?", id);
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return delect;
    }

    @Override
    public int updateStudent(Student student) {
        int update =0;
        try {
             update = queryRunner.update("update from student set name=?,classid=?,password=? where id =?", student.getName(), student.getClassid(), student.getPassword(), student.getId());
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }
        return update;
    }
}
